﻿using log4net.Appender;
using log4net.Config;
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LEDScreenLinkTool.Util
{
    public class LogHelper
    {
        private static log4net.ILog loginfo = null;
        private static string s_PathToContentRoot = string.Empty;
        private static readonly string C_RepositoryName = "NETCoreRepository";
        private static readonly string C_ConfigFilePath = "log4net.config";

        public static void Init()
        {
            try
            {
                s_PathToContentRoot = Directory.GetCurrentDirectory();
                log4net.GlobalContext.Properties["LogsDirectory"] = s_PathToContentRoot;//AppDomain.CurrentDomain.BaseDirectory;
                FileInfo file = new FileInfo(Path.Combine(s_PathToContentRoot, C_ConfigFilePath));
                var repository = LogManager.CreateRepository(C_RepositoryName);
                XmlConfigurator.Configure(repository, file);
                loginfo = log4net.LogManager.GetLogger(C_RepositoryName, "LogInfo");
                var appdenders = repository.GetAppenders();
                DateTime validateDay = DateTime.Today.AddDays(-10);
                foreach (var appdender in appdenders)
                {
                    if (appdender is RollingFileAppender rollApp)
                    {
                        var logDir = Directory.GetParent(rollApp.File);
                        var logFiles = logDir.GetFiles();
                        foreach (var logInfo in logFiles)
                        {
                            if (logInfo.CreationTime < validateDay)
                            {
                                try
                                {
                                    logInfo.Delete();
                                }
                                catch (Exception fiex)
                                {
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        public static void Info(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }
        public static void Info(string excptionType, string message, string stackTrace)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.ErrorFormat("{0}:{1}\n{2}", excptionType, message, stackTrace);
            }
        }
        public static void Debug(string info)
        {
            if (loginfo.IsDebugEnabled)
            {
                loginfo.Debug(info);
            }
        }

        public static void Warn(string info)
        {
            if (loginfo.IsWarnEnabled)
            {
                loginfo.Warn(info);
            }
        }
        /// <summary>
        /// Error 要将信息写入数据表,在transaction.rollback后写入
        /// </summary>
        /// <param name="se"></param>
        public static void Error(Exception se)
        {
            if (loginfo.IsErrorEnabled && se != null)
            {
                loginfo.Error(se);
            }
        }
        public static void Error(object message)
        {
            if (loginfo.IsErrorEnabled && message != null)
            {
                loginfo.Error(message);
            }
        }
        public static void Error(string info, Exception se)
        {
            if (loginfo.IsErrorEnabled && se != null)
            {
                loginfo.Error(info, se);
            }
        }

    }
}
